﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace Dmo_lc_2.Shape
{
    public class PolylineShape : BaseShape
    {
        public bool closed;
        public double length;
        public List<Dictionary<string,object>> vertices;
		// 全局宽度
        public double constantWidth;

        public PolylineShape(Entity in_entity, Transaction tr) : base(in_entity, tr)
        {
        }

        //        Dictionary<string, string> dic = new Dictionary<string, string>();

        public override void Init()
        {
            shapeType = ShapeType.POLYLINE;
            shapeTypeName = shapeType.ToString();

            Polyline in_shape = (Polyline)mEntity;

            color = getColor();

            closed = in_shape.Closed;
            length = in_shape.Length;
            try {
                constantWidth = in_shape.ConstantWidth;
            } catch (Autodesk.AutoCAD.Runtime.Exception e) {
                constantWidth = 1;
            }

            vertices = new List<Dictionary<string, object>> { };
            int numOfVertices = in_shape.NumberOfVertices;
            for (int i = 0; i < numOfVertices; i++)
            {
                Point2d p = in_shape.GetPoint2dAt(i);
                double tempBulge = in_shape.GetBulgeAt(i);
                double[] tempPoint = new double[] { p.X ,p.Y,0 }; // 点坐标z方向取0

                Dictionary<string, object> temp = new Dictionary<string, object> { };
                temp.Add("point", tempPoint);
                temp.Add("bulge", tempBulge);
                temp.Add("startWidth", in_shape.GetStartWidthAt(i));
                temp.Add("endWidth", in_shape.GetEndWidthAt(i));

                vertices.Add(temp);

                SegmentType st = in_shape.GetSegmentType(i);
                temp.Add("segmentType", st);
                if (st == SegmentType.Line)
                {
                    LineSegment2d lineSegment = in_shape.GetLineSegment2dAt(i);
                    temp.Add("startPoint", new double[] { lineSegment.StartPoint.X , lineSegment.StartPoint.Y,0});
                    temp.Add("endPoint", new double[] { lineSegment.EndPoint.X, lineSegment.EndPoint.Y, 0 });

                }
                else if (st == SegmentType.Arc) {
                    CircularArc2d arcSegment =  in_shape.GetArcSegment2dAt(i);

                  //  new double[] { tempEntity.Center.X, tempEntity.Center.Y, tempEntity.Center.Z };
                    temp.Add("center", new double[] { arcSegment.Center.X, arcSegment.Center.Y, 0 });
                    temp.Add("radius", arcSegment.Radius);

                    temp.Add("weight", 1);

                    temp.Add("startAngle", arcSegment.StartAngle);
                    temp.Add("endAngle", arcSegment.EndAngle);
                    temp.Add("isClockWise", arcSegment.IsClockWise);


                    temp.Add("startPoint", new double[] { arcSegment.StartPoint.X, arcSegment.StartPoint.Y, 0 });
                    temp.Add("endPoint", new double[] { arcSegment.EndPoint.X, arcSegment.EndPoint.Y, 0 });

                }
            }

        }
    }
}